package com.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 订单生成器
 *
 * @author admin
 * 2023/06/19
 */
public class OrderGenerator {

    /**
     * 生成订单号
     *
     * @return 不重复的订单号
     */
    public static synchronized String generateOrderNumber(String name) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = dateFormat.format(new Date());
        int random = (int) ((Math.random() + 1) * 1000);
        String orderId = name + dateString + random;
        return (int) ((Math.random() + 1) * 1000) + orderId;
    }

    /**
     * 创建订单号
     *
     * @return {@link Long}
     */
    public static synchronized Long creteOrderNumber() {


        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = dateFormat.format(new Date());

        long random = (long) (int) ((Math.random() + 1) * 1000);

        long orderId = Long.parseLong(dateString + random);
        String i = String.valueOf(((int) ((Math.random() + 1) * 1)));

        i = Long.parseLong(i) + String.valueOf(orderId);
        System.out.println(i);
        return Long.valueOf(i);
    }

}